package com.tian.view.draw.text

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.tian.util.view.DisplayUtil

/**
 * @author tiansongtao
 * @date 2022/12/30 17:23
 */
class DrawTextView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
        View(context, attrs, defStyleAttr) {

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null)

    val paint = Paint().apply {
        color = Color.RED
        strokeWidth = DisplayUtil.dpToPx(1f)
        style = Paint.Style.STROKE
    }
    val textPaint = Paint().apply {
        color = Color.BLACK
        style = Paint.Style.FILL_AND_STROKE
        textSize = DisplayUtil.spToPx(14f)
    }

    /**
     *  drawText(text,x,y,paint) x\y 绘制文字的起始坐标，该坐标为基线起始点
     *  drawText(text,index, count, x,y,paint), index,绘制从第一个字符开始，count：绘制几个字符
     *  drawTextOnPath: 沿着路径绘制文字。
     *
     *  [Paint.setTypeface]: 设置字体样式 Typeface.DEFAULT_BOLD 设置粗体
     *      Typeface.ITALIC: 斜体， Typeface.BOLD_ITALIE: 粗斜体
     *  [Typeface.create]: 使用该函数可以指定字体，但因版权问题不常用
     *      Typeface.create("宋体", Typeface.NORMAL)
     *
     *  [Paint.setTextAlign] 确定字体绘制相对于基线起始点坐标的位置
     *      [Paint.Align.LEFT] [Paint.Align.CENTER] [Paint.Align.RIGHT]
     */
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        textPaint.textSize = DisplayUtil.spToPx(14f)
        canvas.drawText("你好啊", 50f, 100f, textPaint)
        canvas.drawPoint(50f, 100f, paint)
        textPaint.typeface = Typeface.DEFAULT_BOLD
        canvas.drawText("绘制粗体", 50f, 150f, textPaint)
        textPaint.typeface = Typeface.DEFAULT

        /**
         * [Canvas.drawText] x\y 是字体基线的起始点，
         *  从绘制上可以看到 基线 是我们常用的英语四线格中的第三条线，并不是绘制字体的左上角或左下角。
         */
        canvas.drawText("绘制字体基线", 50f, 300f, textPaint)
        canvas.drawText("harvic's blog", 50f, 370f, textPaint)
        canvas.drawLine(50f, 370f, 400f, 370f, paint)


        /**
         * [Paint.Align] 确定文字起始点相对于基线起始坐标的位置
         */
        val alignLeft = 500f
        var hAlign = 450f
        textPaint.textAlign = Paint.Align.CENTER
        canvas.drawText("基线起始位置", alignLeft, hAlign, textPaint)
        textPaint.textAlign = Paint.Align.LEFT
        canvas.drawLine(alignLeft, hAlign, alignLeft, hAlign + 200f, paint)
        hAlign += 60f
        canvas.drawText("Paint.Align.LEFT", alignLeft, hAlign, textPaint)
        textPaint.textAlign = Paint.Align.CENTER
        hAlign += 60f
        canvas.drawText("Paint.Align.CENTER", alignLeft, hAlign, textPaint)
        textPaint.textAlign = Paint.Align.RIGHT
        hAlign += 60f
        canvas.drawText("Paint.Align.RIGHT", alignLeft, hAlign, textPaint)
        textPaint.textAlign = Paint.Align.LEFT


        /**
         * 绘制文字的四格线.
         * [Paint.getFontMetrics] FontMetrics: 文字绘制位置描述 以baseLine为基准
         * [Paint.FontMetrics.top]: 可绘制的最高高度所在线, top=topLine.y - baseLine.y
         * [Paint.FontMetrics.ascent]: 绘制单个字符时，字符应当的最高高度所在线 ascent=ascentLine.y - baseLine.y
         * [Paint.FontMetrics.descent]: 绘制单个字符时，字符应当的最低高度所在线 descent=descentLine.y - baseLine.y
         * [Paint.FontMetrics.bottom]: 可绘制的最底高度所在线 bottom=bottomLine.y - baseLine.y
         *
         */
        var lFont = 50f
        var baseFont = 800f
        textPaint.textSize = DisplayUtil.spToPx(30f)
        val fm = textPaint.fontMetrics
        val log = "当前FontMetrice top:${fm.top} ascent:${fm.ascent}"
        val log1 = "  descent:${fm.descent} bottom:${fm.bottom}"
        Log.i("tiansongtao", "DrawTextView onDraw: $log$log1")
        //字符串高度
        var text = "harvic's blog"
        canvas.drawText(text, lFont, baseFont, textPaint)
        //绘制基线 baseLine
        canvas.drawLine(lFont, baseFont, lFont + 400f, baseFont, paint)
        //绘制基线 topLine
        canvas.drawLine(lFont, baseFont + fm.top, lFont + 400f, baseFont + fm.top, paint)
        //绘制基线 ascentLine
        canvas.drawLine(lFont, baseFont + fm.ascent, lFont + 400f, baseFont + fm.ascent, paint)
        //绘制基线 descentLine
        canvas.drawLine(lFont, baseFont + fm.descent, lFont + 400f, baseFont + fm.descent, paint)
        //绘制基线 bottomLine
        canvas.drawLine(lFont, baseFont + fm.bottom, lFont + 400f, baseFont + fm.bottom, paint)
        val textHeight = baseFont + fm.bottom - (baseFont + fm.top)
        val textWidth = textPaint.measureText(text)
        textPaint.textSize = DisplayUtil.spToPx(14f)
        canvas.drawText(log, lFont, baseFont + 80f, textPaint)
        canvas.drawText(log, lFont, baseFont + 130f, textPaint)
        canvas.drawText("文字高度:$textHeight 文字宽度:$textWidth", lFont, baseFont + 190f, textPaint)

    }
}